<?php

    /**
     *      [Discuz!] (C)2001-2099 Comsenz Inc.
     *      This is NOT a freeware, use is subject to license terms
     *      $Id: forumindex.php 27451 2012-02-01 05:48:47Z monkey $
     *      提交文章评论
     */
    if (!defined('IN_MOBILE_API')) {
        exit('Access Denied');
    }

    include_once 'forum.php';

    class cloud_api {

        function common() {

        }

        function output() {
//            if(empty($_GET['appid'])){
//                //正常返回
//                $variable = array(
//                    'code'=>1001,
//                    'messagestr' => '缺少参数：appid!',
//                );
//                cloud_core::result (cloud_core::variable ($variable));
//            }
            //1,获取正常状态的uid
//            $data = C::t('#cloud#cloud_ios_push')->fetch_all_by_caid($_GET['appid']);
//            //2,循环未读消息进行推送
//            if(!empty($data)){
//                foreach ($data as $key=>$val) {
//                    $token = $val['token'];
//                    self::push_message($token,0,$_GET['appid']);
//                }
//            }

            $msg_content =array('n_title'=>'通知','n_content'=>'内容');
            $str = self::send($appkeys='',$masterSecret='',$sendno = 1,$receiver_type = 4,$receiver_value = '', $msg_type = 1, $msg_content, $platform = 'android');

            //正常返回
            $variable = array(
                'messagestr' => $str,
            );
            cloud_core::result (cloud_core::variable ($variable));
        }
        function request_post($url = '', $param = ''){
            if (empty($url) || empty($param)) {
                return false;
            }
            $postUrl = $url;
            $curlPost = $param;
            $ch = curl_init();//初始化curl
            curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
            curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
            curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
            //echo $url;echo $curlPost;exit;
            $data = curl_exec($ch);//运行curl
            curl_close($ch);
            return $data;
        }

        function send($appkeys,$masterSecret,$sendno = 1,$receiver_type = 4, $receiver_value = array(), $msg_type = 1, $msg_content = array(), $platform = 'android') {

            //        $msg_content =array('n_title'=>'通知','n_content'=>'内容');
            //        $content = json_encode($msg_content,true);
            //        $receiver_value =array(1,2,3);
            //        $receiver_value = implode(',',$receiver_value);
            $url = 'http://api.jpush.cn:8800/sendmsg/v2/sendmsg';
            $param = '';
            $param .= '&sendno='.$sendno;
            $appkeys = empty($appkeys)? '217f48a6776317af7ac9c517' : $appkeys;
            $param .= '&app_key='.$appkeys;
            $param .= '&receiver_type='.$receiver_type;
            if($receiver_type==3 && !empty($receiver_value)){
                $receiver_value = implode(',',$receiver_value);
                $param .= '&receiver_value='.$receiver_value;
            }
            $masterSecret = empty($masterSecret)? 'cc64be722d75809a70be204e' : $masterSecret;
            $verification_code = md5($sendno.$receiver_type.$receiver_value.$masterSecret);
            $param .= '&verification_code='.$verification_code;
            $param .= '&msg_type='.$msg_type;
            $param .= '&msg_content='.json_encode($msg_content,true);
            $param .= '&platform='.$platform;
            $res = self::request_post($url, $param);
            if ($res === false) {
                return false;
            }
            $res_arr = json_decode($res, true);
            $res_arr['errmsg']= "没有错误信息";
            switch (intval($res_arr['errcode'])) {
                case 0:
                    $res_arr['errmsg'] = '发送成功';
                    //echo '发送成功';
                    break;
                case 10:
                    $res_arr['errmsg'] = '系统内部错误';
                    //echo '系统内部错误';
                    break;
                case 1001:
                    $res_arr['errmsg'] = '只支持 HTTP Post 方法，不支持 Get 方法';
                    //echo '只支持 HTTP Post 方法，不支持 Get 方法';
                    break;
                case 1002:
                    $res_arr['errmsg'] = '缺少了必须的参数';
                    //echo '缺少了必须的参数';
                    break;
                case 1003:
                    $res_arr['errmsg'] = '参数值不合法';
                    //echo '参数值不合法';
                    break;
                case 1004:
                    $res_arr['errmsg'] = '验证失败';
                    //echo '验证失败';
                    break;
                case 1005:
                    $res_arr['errmsg'] = '消息体太大';
                    //echo '消息体太大';
                    break;
                case 1007:
                    $res_arr['errmsg'] = 'receiver_value 参数 非法';
                    //echo 'receiver_value 参数 非法';
                    break;
                case 1008:
                    $res_arr['errmsg'] = 'appkey参数非法';
                    //echo 'appkey参数非法';
                    break;
                case 1010:
                    $res_arr['errmsg'] = 'msg_content 不合法';
                    //echo 'msg_content 不合法';
                    break;
                case 1011:
                    $res_arr['errmsg'] = '没有满足条件的推送目标';
                    //echo '没有满足条件的推送目标';
                    break;
                case 1012:
                    $res_arr['errmsg'] = 'iOS 不支持推送自定义消息。只有 Android 支持推送自定义消息';
                    //echo 'iOS 不支持推送自定义消息。只有 Android 支持推送自定义消息。';
                    break;
                default:
                    //echo '调用成功';
                    break;
            }
            $str= "第".$res_arr['sendno']."条发送".$res_arr['errmsg']."！";
            return $str;
        }
    }

?>